package fr.umlv.m2.jee.pmlv.web.professor;

import fr.umlv.m2.jee.pmlv.persistence.planning.RecordDate;
import fr.umlv.m2.jee.pmlv.persistence.subject.Exam;
import fr.umlv.m2.jee.pmlv.persistence.subject.Lesson;
import fr.umlv.m2.jee.pmlv.persistence.subject.Project;
import fr.umlv.m2.jee.pmlv.persistence.subject.TD;
import fr.umlv.m2.jee.pmlv.persistence.subject.TP;
import fr.umlv.m2.jee.pmlv.service.planning.DisplayRecordDate;
import fr.umlv.m2.jee.pmlv.service.planning.IDisplayRecordDateService;
import fr.umlv.m2.jee.pmlv.service.subject.DisplayAbstractTheme;
import fr.umlv.m2.jee.pmlv.service.subject.IDisplayAbstractThemeService;
import fr.umlv.m2.jee.pmlv.service.subject.IDisplaySubjectService;
import fr.umlv.m2.jee.pmlv.service.user.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Servlet wich represents page for a professor where he can add or edit a theme.
 * @author Stephanie
 */
public class EditTheme extends HttpServlet {
    // Variables
    private User user = null;   //Contains informations about user
    private List<Integer> years = new ArrayList<Integer>(); //Contains years when professor teaching a subject
    private String typeTheme = "";
    private String action = "";
    private long idTheme = -1;
    private DisplayAbstractTheme theme = null;
    private String errors = "";
    private String back = "";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditTheme() {
            super();
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response).
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request,response);
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response).
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //Data
        user = null;
        years.clear();
        action = "";
        typeTheme = "";
        idTheme = -1;
        theme = null;
        errors = "";
        back = "";

        //Retrieve informations about user
        HttpSession session = request.getSession(true);
        user = (User)session.getAttribute("user");
        years.addAll((List<Integer>)session.getAttribute("years"));
        back = (String)session.getAttribute("back");

        //Retrieve informations about theme
        action = request.getParameter("action");
        typeTheme = request.getParameter("typeTheme");
        ServletContext context = getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        IDisplayAbstractThemeService themeService = (IDisplayAbstractThemeService) applicationContext.getBean("defautDisplayAbstractThemeService");
        if(request.getParameter("theme")!=null)
        {
            idTheme = Integer.parseInt(request.getParameter("theme"));
            theme = themeService.getAbstractThemeById(idTheme);
        }

        //Redirect to previous page
        if(request.getParameter("ok")!=null)
        {
            boolean check = false;
            if(action.equals("edit"))
                editTheme(request, response);
            else if(action.equals("add"))
                addTheme(request, response);
            if(errors.length()==0) response.sendRedirect((String)session.getAttribute("back"));
        }
        else if(request.getParameter("cancel")!=null)
            response.sendRedirect(back);


        //View
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
        out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\" >");

        out.println(" <head>");
        out.println("   <title>PMLV : Portail Universitaire de Marne-La-Vall&eacute;e</title>");
        out.println("   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />");
        out.println("   <link rel=\"stylesheet\" media=\"screen\" type=\"text/css\" href=\"../css/professor/professor.css\" />");
        out.println("   <link rel=\"stylesheet\" media=\"screen\" type=\"text/css\" href=\"../css/professor/editTheme.css\" />");
        out.println(" </head>");

        out.println(" <body>");

        out.println("   <div id=\"header\">");
        out.println("     <ul>");
        out.println("       <li class=\"menuItem menuItemSpacing\"><a href=\"../Professor\">Accueil</a></li>");
        out.println("       <li class=\"menuItem menuItemSpacing\"><a href=\"ListOfStudents\">Listes des &eacute;tudiants</a></li>");
        out.println("       <li class=\"menuItem menuItemSpacing cursor\">Emploi du temps");
        out.println("         <ul class=\"subMenuBar\">");
        out.println("           <li class=\"subMenu\"><a href=\"ScheduleOfCoursesAndExams\">Cours et examens</a></li>");
        out.println("           <li class=\"subMenu\"><a href=\"ScheduleOfWorksToMake\">Rendu de travaux</a></li>");
        out.println("         </ul>");
        out.println("       </li>");
        out.println("       <li class=\"menuItem menuItemSpacing cursor\">Gestion de mati&egrave;re");
        out.println("         <ul class=\"subMenuBar\">");
        for(Integer year:years)
        {
            out.println("           <li class=\"subMenu\"><a href=\"ManagementOfSubjects?year="+year+"\">"+ year +"-"+ (year+1) +"</a></li>");
        }
        out.println("         </ul>");
        out.println("       </li>");
        out.println("       <li id=\"menuItemMargin\" class=\"menuItem\"><a href=\"../j_spring_security_logout\">Se d&eacute;connecter</a></li>");
        out.println("     </ul>");
        out.println("   </div>");

        if(action.equals("add"))
            getDivContentForAdd(out, request, response);
        else if(action.equals("edit"))
            getDivContentForEdit(out, request, response);

        out.println("   <div id=\"footer\">");
        out.println("     <div id=\"copyright\">&copy; Universit&eacute; Paris-Est Marne-la-Vall&eacute;e</div>");
        out.println("     <div>PMLV v1.0 </div>");
        out.println("   </div>");

        out.println(" </body>");

        out.println("</html>");
        out.close();
    }

    /**
     *  Content of page
     */
    private void getDivContentForAdd(PrintWriter out, HttpServletRequest request, HttpServletResponse response)
    {
        String str_typeTheme = "";
        if(typeTheme.equals("lesson")) str_typeTheme = "d'un cours";
        else if(typeTheme.equals("td")) str_typeTheme = "d'un travaux dirig&eacute;s";
        else if(typeTheme.equals("tp"))  str_typeTheme = "d'un travaux pratiques";
        else if(typeTheme.equals("project"))  str_typeTheme = "d'un projet";
        else if(typeTheme.equals("exam"))  str_typeTheme = "d'un examen";

        out.println("   <div id=\"content\">");
        out.println("     <div id=\"title\"> Ajout "+str_typeTheme+" </div>");

        out.println("       <form method=\"get\" action=\"EditTheme\">");
        out.println("       <table id=\"EditThemeTable\">");
        
        //Errors
        if(errors.length()>0)
        {
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
            out.println("           <tr> <td class=\"error\" colspan=\"2\"> Vous n'avez pas rempli certains champs correctement :<br/>"+errors+" </td> </tr>");
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
        }
        
        //Details
        out.println("           <tr> <td colspan=\"2\" class=\"titre\"> Details </td> </tr>");
        out.println("           <tr>");
        out.println("               <td class=\"name\"> Sujet : </td>");
        out.println("               <td class=\"input\"> <input type=\"text\" size=75 name=\"sujet\"> </td>");
        out.println("           </tr> ");
        out.println("           <tr>");
        out.println("               <td class=\"name\"> Publier : </td>");
        out.println("               <td class=\"input\"> <input type=\"checkbox\" name=\"publish\" value=\"1\" unchecked> </td> ");
        out.println("           </tr> ");
        out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");

        //To do
        if(typeTheme.equals("td") || typeTheme.equals("tp") || typeTheme.equals("project"))
        {
            out.println("           <tr>");
            out.println("               <td class=\"name\"> A Rendre : </td>");
            out.println("               <td class=\"input\"> <input type=\"checkbox\" name=\"todo\" value=\"1\" unchecked> </td>");
            out.println("           </tr> ");
            out.println("           <tr>");
            out.println("               <td class=\"name\"> Date : </td>");
            out.println("               <td class=\"input\"> <input type=\"text\" size=\"2\" name=\"day\" value=\"jj\"> / ");
            out.println("               <input type=\"text\" size=\"2\" name=\"month\" value=\"mm\"> / ");
            out.println("               <input type=\"text\" size=\"4\" name=\"year\" value=\"aaaa\"> &agrave; ");
            out.println("               <input type=\"text\" size=\"2\" name=\"hour\" value=\"hh\"> h ");
            out.println("               <input type=\"text\" size=\"4\" name=\"minute\" value=\"mm\"> </td> ");
            out.println("           </tr> ");
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
        }

        //Pdf
        out.println("           <tr> <td colspan=\"2\" class=\"titre\"> Au format Pdf </td> </tr>");
        out.println("           <tr>");
        out.println("               <td class=\"name\"> Chemin du fichier : </td>");
        out.println("               <td class=\"input\"> <input type=\"text\" size=\"75\" name=\"pathPdf\"> </td>");
        out.println("           </tr> ");
        out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");

        //Html
        if(typeTheme.equals("td") || typeTheme.equals("tp") || typeTheme.equals("project"))
        {
            out.println("           <tr> <td colspan=\"2\" class=\"titre\"> Au format Html </td> </tr>");
            out.println("           <tr>");
            out.println("               <td class=\"name\"> Chemin du fichier : </td>");
            out.println("               <td class=\"input\"> <input type=\"text\" size=\"75\" name=\"pathHtml\"> </td>");
            out.println("           </tr> ");
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
        }

        //Button
        out.println("           <tr> <td colspan=\"2\" clas=\"button\"> <center>");
        out.println("                   <input type=\"submit\" name=\"ok\" value=\"Ajouter\"> &nbsp ");
        out.println("                   &nbsp <input type=\"submit\" name=\"cancel\" value=\"Annuler\">");
        out.println("                   <input type=\"hidden\" name=\"action\" value=\""+action+"\">");
        out.println("                   <input type=\"hidden\" name=\"typeTheme\" value=\""+typeTheme+"\">");
        out.println("                   <input type=\"hidden\" name=\"theme\" value=\""+idTheme+"\">");
        out.println("           </center> </td> </tr>");
        
        out.println("       </table>");
        out.println("       </form>");

        out.println("   </div>");
    }

    /**
     *  Content of page
     */
    private void getDivContentForEdit(PrintWriter out, HttpServletRequest request, HttpServletResponse response)
    {
        String str_typeTheme = "";
        if(typeTheme.equals("lesson")) str_typeTheme = "d'un cours";
        else if(typeTheme.equals("td")) str_typeTheme = "d'un travaux dirig&eacute;s";
        else if(typeTheme.equals("tp"))  str_typeTheme = "d'un travaux pratiques";
        else if(typeTheme.equals("project"))  str_typeTheme = "d'un projet";
        else if(typeTheme.equals("exam"))  str_typeTheme = "d'un examen";


        out.println("   <div id=\"content\">");
        out.println("     <div id=\"title\"> &Eacute;dition "+str_typeTheme+" </div>");

        out.println("       <form method=\"get\" action=\"EditTheme\">");
        out.println("       <table id=\"EditThemeTable\">");

        //Errors
        if(errors.length()>0)
        {
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
            out.println("           <tr> <td class=\"error\" colspan=\"2\"> Vous n'avez pas rempli certains champs correctement :<br/>"+errors+" </td> </tr>");
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
        }

        //Details
        out.println("           <tr> <td colspan=\"2\" class=\"titre\"> Details </td> </tr>");
        out.println("           <tr>");
        out.println("               <td class=\"name\"> Sujet : </td>");
        out.println("               <td class=\"input\"> <input type=\"text\" size=75 name=\"sujet\" value=\""+ (theme.getName()!=null ? theme.getName() : "") +"\"> </td>");
        out.println("           </tr> ");
        out.println("           <tr>");
        out.println("               <td class=\"name\"> Publier : </td>");
        out.println("               <td class=\"input\"> <input type=\"checkbox\" name=\"publish\" value=\"1\" "+ (theme.getPublished()==1 ? "checked" : "unchecked") +" > </td> ");
        out.println("           </tr> ");
        out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");

        //To do
        if(typeTheme.equals("td") || typeTheme.equals("tp") || typeTheme.equals("project"))
        {
            out.println("           <tr>");
            out.println("               <td class=\"name\"> A Rendre : </td>");
            out.println("               <td class=\"input\"> <input type=\"checkbox\" name=\"todo\" value=\"1\" "+ (theme.getRecordDate()!=null ? "checked" : "unchecked") +" > </td>");
            out.println("           </tr> ");
            out.println("           <tr>");
            out.println("               <td class=\"name\"> Date : </td>");
            out.println("               <td class=\"input\"> <input type=\"text\" size=\"2\" name=\"day\" value=\""+ (theme.getRecordDate()!=null ? ((theme.getRecordDate().getDeadLine().getDate())/10==0 ? "0"+(theme.getRecordDate().getDeadLine().getDate()) : (theme.getRecordDate().getDeadLine().getDate())) : "jj") +"\"> / ");
            out.println("               <input type=\"text\" size=\"2\" name=\"month\" value=\""+ (theme.getRecordDate()!=null ? ((theme.getRecordDate().getDeadLine().getMonth()+1)/10==0 ? "0"+(theme.getRecordDate().getDeadLine().getMonth()+1) : (theme.getRecordDate().getDeadLine().getMonth()+1)) : "mm") +"\"> / ");
            out.println("               <input type=\"text\" size=\"4\" name=\"year\" value=\""+ (theme.getRecordDate()!=null ? theme.getRecordDate().getDeadLine().getYear()+1900 : "aaaa") +"\"> &agrave; ");
            out.println("               <input type=\"text\" size=\"2\" name=\"hour\" value=\""+ (theme.getRecordDate()!=null ? ((theme.getRecordDate().getDeadLine().getHours())/10==0 ? "0"+(theme.getRecordDate().getDeadLine().getHours()) : (theme.getRecordDate().getDeadLine().getHours())) : "hh") +"\"> h ");
            out.println("               <input type=\"text\" size=\"2\" name=\"minute\" value=\""+ (theme.getRecordDate()!=null ? ((theme.getRecordDate().getDeadLine().getMinutes())/10==0 ? "0"+(theme.getRecordDate().getDeadLine().getMinutes()) : (theme.getRecordDate().getDeadLine().getMinutes())) : "mm") +"\"> </td> ");
            out.println("           </tr> ");
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
        }

        //Pdf
        out.println("           <tr> <td colspan=\"2\" class=\"titre\"> Au format Pdf </td> </tr>");
        out.println("           <tr>");
        out.println("               <td class=\"name\"> Chemin du fichier : </td>");
        out.println("               <td class=\"input\"> <input type=\"text\" size=\"75\" name=\"pathPdf\" value=\""+ (theme.getPdfFileName()!=null ? theme.getPdfFileName() : "") +"\"> </td>");
        out.println("           </tr> ");
        out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");

        //Html
        if(typeTheme.equals("td") || typeTheme.equals("tp") || typeTheme.equals("project"))
        {
            out.println("           <tr> <td colspan=\"2\" class=\"titre\"> Au format Html </td> </tr>");
            out.println("           <tr>");
            out.println("               <td class=\"name\"> Chemin du fichier : </td>");
            out.println("               <td class=\"input\"> <input type=\"text\" size=\"75\" name=\"pathHtml\" value=\""+ (theme.getHtmlFileName()!=null ? theme.getHtmlFileName() : "") +"\"> </td>");
            out.println("           </tr> ");
            out.println("           <tr> <td colspan=\"2\"> &nbsp </td> </tr> ");
        }

        //Button
        out.println("           <tr> <td colspan=\"2\" clas=\"button\"> <center>");
        out.println("                   <input type=\"submit\" name=\"ok\" value=\"&Eacute;diter\"> &nbsp ");
        out.println("                   &nbsp <input type=\"submit\" name=\"cancel\" value=\"Annuler\">");
        out.println("                   <input type=\"hidden\" name=\"action\" value=\""+action+"\">");
        out.println("                   <input type=\"hidden\" name=\"typeTheme\" value=\""+typeTheme+"\">");
        out.println("                   <input type=\"hidden\" name=\"theme\" value=\""+idTheme+"\">");
        out.println("           </center> </td> </tr>");

        out.println("       </table>");
        out.println("       </form>");
        
        out.println("   </div>");
    }

    /**
     * Method to check error on field.
     */
    public boolean checkFields(HttpServletRequest request, HttpServletResponse response)
    {
        if(request.getParameter("sujet").length()==0)
            errors=errors+" - Le nom du sujet doit &ecirc;tre obligatoirement rempli.<br/>";
        if(request.getParameter("pathPdf").length()!=0 && ( request.getParameter("pathPdf").indexOf("http://")!=0 || request.getParameter("pathPdf").indexOf(".pdf",request.getParameter("pathPdf").length()-4)==-1 ))
            errors=errors+" - Le chemin du fichier pdf doit &ecirc;tre une adresse internet pointant vers un fichier \".pdf\".<br/>";
        if(typeTheme.equals("td") || typeTheme.equals("tp") || typeTheme.equals("project"))
        {
            if(request.getParameter("pathHtml").length()!=0 && ( request.getParameter("pathHtml").indexOf("http://")!=0 || request.getParameter("pathHtml").indexOf(".html",request.getParameter("pathHtml").length()-5)==-1 ))
                errors=errors+" - Le chemin du fichier html doit &ecirc;tre une adresse internet pointant vers un fichier \".html\" .<br/>";
            Date checkDate = null;
            if(request.getParameter("todo")!=null)
            {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                String str_date = request.getParameter("day")+"/"+request.getParameter("month")+"/"+request.getParameter("year")+"4 "+request.getParameter("hour")+":"+request.getParameter("minute");
                try {
                    checkDate = sdf.parse(str_date);
                }
                catch (ParseException e) {
                    errors=errors+" - Les champs pour la date de rendu doivent &ecirc;tre compos&eacute;s de nombres uniquement.<br/>";
                    return (errors.length()==0);
                }
                if( request.getParameter("day").length()!=2 || request.getParameter("month").length()!=2 || request.getParameter("year").length()!=4 || request.getParameter("hour").length()!=2 || request.getParameter("minute").length()!=2 )  {
                    errors=errors+" - Les champs pour la date de rendu doivent respect&eacute;s le format suivant : jj/mm/aaaa &agrave; hh h mm<br/>";
                    return (errors.length()==0);
                }

                if (!sdf.format(checkDate).equals(str_date)) {
                    errors=errors+" - La date de rendu n'est pas valide.<br/>";
                    return (errors.length()==0);
                }

            }
        }
        return (errors.length()==0);
    }

    /**
     * Method to edit a theme
     */
    public void editTheme(HttpServletRequest request, HttpServletResponse response)
    {
        if(!checkFields(request, response))
            return;

        ServletContext context = getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        IDisplayAbstractThemeService themeService = (IDisplayAbstractThemeService) applicationContext.getBean("defautDisplayAbstractThemeService");
        IDisplayRecordDateService dateService = (IDisplayRecordDateService) applicationContext.getBean("defautDisplayRecordDateService");

        if(typeTheme.equals("td") || typeTheme.equals("tp") || typeTheme.equals("project"))
        {
            theme.setHtmlFilename( (request.getParameter("pathHtml").length()==0 ? null : request.getParameter("pathHtml") ) );
            theme.setName(request.getParameter("sujet"));
            theme.setPdfFilename( (request.getParameter("pathPdf").length()==0 ? null : request.getParameter("pathPdf") ) );
            theme.setPublished( (request.getParameter("publish")!=null ? 1 : 0) );
            if(request.getParameter("todo")==null && theme.getRecordDate()!=null)
            {
                DisplayRecordDate record = new DisplayRecordDate(theme.getRecordDate());
                dateService.deleteRecordDate(record);
                theme.setRecordDate(null);
                themeService.editAbstractTheme(theme);
            }
            else if(request.getParameter("todo")!=null)
            {
                if(theme.getRecordDate()!=null)
                {
                    DisplayRecordDate record = new DisplayRecordDate(theme.getRecordDate());
                    record.setDeadLine(new Date( Integer.parseInt(request.getParameter("year"))-1900, Integer.parseInt(request.getParameter("month"))-1, Integer.parseInt(request.getParameter("day")), Integer.parseInt(request.getParameter("hour")), Integer.parseInt(request.getParameter("minute"))));
                    dateService.editRecordDate(record);
                }
                else
                {
                    DisplayRecordDate record = new DisplayRecordDate(new RecordDate());
                    record.setTheme(theme.getAbstractTheme());
                    record.setDeadLine(new Date( Integer.parseInt(request.getParameter("year"))-1900, Integer.parseInt(request.getParameter("month"))-1, Integer.parseInt(request.getParameter("day")), Integer.parseInt(request.getParameter("hour")), Integer.parseInt(request.getParameter("minute"))));
                    dateService.addRecordDate(record);
                    theme.setRecordDate(record.getRecordDate());
                }
                themeService.editAbstractTheme(theme);
            }
        }
        else
        {
            theme.setName(request.getParameter("sujet"));
            theme.setPdfFilename( (request.getParameter("pathPdf").length()==0 ? null : request.getParameter("pathPdf") ) );
            theme.setPublished( (request.getParameter("publish")!=null ? 1 : 0) );
            themeService.editAbstractTheme(theme);
        }
    }

    /**
     * Method to check info and add a theme
     */
    public void addTheme(HttpServletRequest request, HttpServletResponse response)
    {
        if(!checkFields(request, response))
            return;

        ServletContext context = getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        IDisplayAbstractThemeService themeService = (IDisplayAbstractThemeService) applicationContext.getBean("defautDisplayAbstractThemeService");
        IDisplayRecordDateService dateService = (IDisplayRecordDateService) applicationContext.getBean("defautDisplayRecordDateService");
        IDisplaySubjectService subjectService = (IDisplaySubjectService) applicationContext.getBean("defautDisplaySubjectService");

        if(typeTheme.equals("td") || typeTheme.equals("tp") || typeTheme.equals("project"))
        {
            DisplayAbstractTheme display = null;
            if(typeTheme.equals("td")) display = new DisplayAbstractTheme(new TD());
            else if(typeTheme.equals("tp")) display = new DisplayAbstractTheme(new TP());
            else if(typeTheme.equals("project")) display = new DisplayAbstractTheme(new Project());
            //themeService.addAbstractTheme(display);
            display.setHtmlFilename( (request.getParameter("pathHtml").length()==0 ? null : request.getParameter("pathHtml") ) );
            display.setName(request.getParameter("sujet"));
            display.setPdfFilename( (request.getParameter("pathPdf").length()==0 ? null : request.getParameter("pathPdf") ) );
            display.setPublished( (request.getParameter("publish")!=null ? 1 : 0) );
            String year = back.substring( back.indexOf("year"), back.indexOf("&", back.indexOf("year"))).replace("year=","");
            String sub = back.substring( back.indexOf("subject"), back.indexOf("&", back.indexOf("subject"))).replace("subject=","");
            display.setYear(Integer.parseInt(year));
            display.setSubject(subjectService.getDisplaySubjectbyId(Long.parseLong(sub)).getSubject());
            themeService.addAbstractTheme(display);
            
            if(request.getParameter("todo")==null)
                display.setRecordDate(null);
            else
            {
                DisplayRecordDate record = new DisplayRecordDate(new RecordDate());
                record.setTheme(display.getAbstractTheme());
                record.setDeadLine(new Date( Integer.parseInt(request.getParameter("year"))-1900, Integer.parseInt(request.getParameter("month"))-1, Integer.parseInt(request.getParameter("day")), Integer.parseInt(request.getParameter("hour")), Integer.parseInt(request.getParameter("minute"))));
                dateService.addRecordDate(record);
                display.setRecordDate(record.getRecordDate());
            }
            themeService.editAbstractTheme(display);
        }
        else
        {
            DisplayAbstractTheme display = null;
            if(typeTheme.equals("lesson")) display = new DisplayAbstractTheme(new Lesson());
            else if(typeTheme.equals("exam")) display = new DisplayAbstractTheme(new Exam());
            display.setHtmlFilename(null);
            display.setName(request.getParameter("sujet"));
            display.setPdfFilename( (request.getParameter("pathPdf").length()==0 ? null : request.getParameter("pathPdf") ) );
            display.setPublished( (request.getParameter("publish")!=null ? 1 : 0) );
            display.setRecordDate(null);
            String year = back.substring( back.indexOf("year"), back.indexOf("&", back.indexOf("year"))).replace("year=","");
            String sub = back.substring( back.indexOf("subject"), back.indexOf("&", back.indexOf("subject"))).replace("subject=","");
            display.setYear(Integer.parseInt(year));
            display.setSubject(subjectService.getDisplaySubjectbyId(Long.parseLong(sub)).getSubject());
            themeService.addAbstractTheme(display);
        }
    }
}
